package com.jian.sys.service.impl;

import com.jian.sys.pojo.Role;
import com.jian.sys.mapper.RoleMapper;
import com.jian.sys.service.RoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jian.sys.mapper.RoleMapper;
import com.jian.sys.service.RoleService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 剑
 * @since 2020-01-13
 */
@Service
@Transactional
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {
    @Override
    public boolean removeById(Serializable id) {
        RoleMapper baseMapper = this.getBaseMapper();
        //根据角色id删除对应sys_role_permission
        baseMapper.deletePermissionByRid(id);
        //根据角色id删除对应sys_role_user
        baseMapper.deleteUserByRid(id);
        return super.removeById(id);
    }

    @Override
    public List<Integer> queryRolePermissionByRid(Integer roleId) {
        return this.getBaseMapper().queryRolePermissionByRid(roleId);
    }
    //保存分配的权限
    @Override
    public void saveSelectPermission(Integer rid, Integer[] ids) {
        RoleMapper baseMapper = this.getBaseMapper();
        //先删除 主键唯一
        baseMapper.deleteRolePermissionById(rid);
        if(ids.length>0&&rid!=null){
            for (Integer pid:ids) {
                this.getBaseMapper().saveSelectPermissionRole(rid,pid);
            }
        }

    }
}
